%{
/*
   Lexer for configuration file specification of Milestone 1 - ECE297.
 */

#include "config_parser.tab.h"
#include <string.h>
#include <stdio.h>
#include "storage.h" 
int lineLength = 0;
int tempAdd = 0; 
%}
%option noyywrap

/* Define macros for use below */
a2Z  [a-zA-Z0-9./]
host server_host
port server_port 
dir data_directory



%%

username	{
   printf("LEX: Username \n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
		  return USERNAME_PROPERTY;
		}

password	{
    printf("LEX:Password \n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
		  return PASSWORD_PROPERTY;

		}

{host}		 { 
    printf("LEX: Host  \n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
		   return HOST_PROPERTY;
		 }

{port}		 { 
    printf("LEX: Port  \n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
		   return PORT_PROPERTY;
		 }

table		 { 
    printf("LEX: Table  \n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
		   return TABLE;
		 }

{dir}		 {
    printf("LEX: Data Directory  \n");
 		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
		   return DDIR_PROPERTY;
		 }

"har["		{
		printf("LEX: CHARACTER_WITH_BRACKET \n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
                   yylval.sval = (char *)strdup(yytext);
		return 	CHARACTER_WITH_BRACKET;
		}

{a2Z}*":c"	{
		printf("LEX: IMPLICIT_KEY_WITH_COLON \n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
                   yylval.sval = (char *)strdup(yytext);

		    return IMPLICIT_KEY_WITH_COLON  ;
		}

{a2Z}*":i"	{
		printf("LEX: IMPLICIT_KEY_WITH_COLON \n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
                   yylval.sval = (char *)strdup(yytext);
		    return IMPLICIT_KEY_WITH_COLON  ;
		}


"nt"		{
		printf("LEX: INTEGER\n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
                   yylval.sval = (char *)strdup(yytext);
		   return INTEGER;
		}
	
[0-9]+"]"	{
		printf("LEX: LENGTH\n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
                   yylval.sval = (char *)strdup(yytext);
		   return LENGTH;
		}

[,]		{
		printf("LEX: COMMA\n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
		  return COMMA;
		}


[\t ]+         { /* ignore white space if more than 1 
			prev was [\t\n ]+		
			*/
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
    printf("LEX: >= 1Whitespace\n"); 
	/* Note: As long don't return, won't affect BISON */
                 }

\n		{
		    printf("LEX: NEWLINE\n"); 
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
			printf("LINELENGTH IS %d\n\n\n\n\n", lineLength);
			if (lineLength > MAX_CONFIG_LINE_LEN)
			{
				printf("config line exceeded MAX_CONFIG_LINE_LEN\n"); 
				return -1; 
			}
			lineLength = 0; 
		    return NEWLINE;
		}
 
#.*\n	         { /* ignore comments; text starting with '#' to 
                      the end of the line */
   printf("LEX: comments \n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
			printf("LINELENGTH IS %d\n\n\n\n\n", lineLength);
			if (lineLength > MAX_CONFIG_LINE_LEN)
			{
				printf("config line exceeded MAX_CONFIG_LINE_LEN\n"); 
				return -1; 
			}
			lineLength = 0; 
                  }
[0-9]+           { /* probably the port number */
   printf("LEX: Port Number \n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
                   yylval.pval = (int) atoi(yytext);
		   return PORT_NUMBER;
                  }
                 
{a2Z}*		 { /* probably a string value but without spaces*/
   printf("LEX: String \n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
                   yylval.sval = (char *)strdup(yytext);
		   return STRING;
                 }

                

                 
[^","]         { 
 printf		("LEX: A character \n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
                   return yytext[0]; 
                  }

<<EOF>>           { 
  printf("LEX: End of File\n");
		         yylval.sval = (char *)strdup(yytext);
			tempAdd = strlen(yylval.sval); 
			lineLength += tempAdd; 
			printf("LINELENGTH IS %d\n\n\n\n\n", lineLength);
			if (lineLength > MAX_CONFIG_LINE_LEN)
			{
				printf("config line exceeded MAX_CONFIG_LINE_LEN\n"); 
				return -1; 
			}
			lineLength = 0; 
		    return ENDOFFILE; 
                    return 0;
                  }
%%

